import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Layout from "../components/layout/index.vue";
import { ElMessage } from "element-plus";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "Layout",
    component: Layout,
    children: [
      {
        path: "/userInfoView",
        name: "userInfoView",
        component: () => import("../views/user/account/index.vue"),
      },
      {
        path: "/sudentInfoView",
        name: "sudentInfoView",
        component: () => import("../views/user/student/index.vue"),
      },
      {
        path: "/teacherInfoView",
        name: "teacherInfoView",
        component: () => import("../views/user/teacher/index.vue"),
      },
      {
        path: "/majorview",
        name: "majorview",
        component: () => import("../views/other/major/index.vue"),
      },
      {
        path: "/scoreview",
        name: "scoreview",
        component: () => import("../views/score/index.vue"),
      },
      {
        path: "/collageview",
        name: "collageview",
        component: () => import("../views/other/collage/index.vue"),
      },
      {
        path: "/classview",
        name: "classview",
        component: () => import("../views/other/clazz/index.vue"),
      },
      {
        path: "/activeview",
        name: "activeview",
        component: () => import("../views/baiye/active/index.vue"),
      },
      {
        path: "/accountview",
        name: "accountview",
        component: () => import("../views/baiye/score/index.vue"),
      },
      {
        path: "/noticeview",
        name: "noticeview",
        component: () => import("../views/notice/index.vue"),
      },
      {
        path: "/carouselview",
        name: "carouselview",
        component: () => import("../views/carousel/index.vue"),
      },
      {
        path: "/recruitview",
        name: "recruitview",
        component: () => import("../views/recruit/index.vue"),
      },
      {
        path: "/postbarview",
        name: "postbarview",
        component: () => import("../views/postbar/index.vue"),
      },
      {
        path: "/goodsview",
        name: "goodsview",
        component: () => import("../views/used/index.vue"),
      },
      {
        path: "/enrollview",
        name: "enrollview",
        component: () => import("../views/baiye/enroll/index.vue"),
      },
      {
        path: "/:catchAll(.*)*",
        name: "NotFound",
        component: () => import("../components/error/404.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/login/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    const token = window.localStorage.getItem("token");
    if (token) {
      next();
    } else {
      ElMessage.error("登录状态无效，请重新登录");
      next("/login");
    }
  } else {
    next();
  }
});
export default router;
